package com.ericsson.enap.modules.autoCheck.job;

import com.ericsson.common.quartz.job.MainJob;
import com.ericsson.common.quartz.job.ManualQuartzJob;
import com.ericsson.common.quartz.service.QuartzTableManager;
import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.SpringUtils;
import com.ericsson.enap.modules.autoCheck.service.DictEopsService;
import com.ericsson.enap.modules.autoCheck.task.EopsTemplateScheduleTask;
import com.ericsson.epc.modules.sys.entity.Dict;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class EopsTemplateScheduleJob  implements MainJob {

    @Autowired
    QuartzTableManager quartzTableManager;

    @Value("${autoCheckPort}")
    private String autoCheckPort;

    @Autowired
    private DictEopsService dictService;

    /*@Autowired
    private EopsTemplateScheduleTask eopsTask;*/

    @Override
    public void doTask() {
        System.out.println("EopsTemplateScheduleJob start.....");

        /*EopsTemplateScheduleTask eopsTask = SpringUtils.getBean("eopsTemplateScheduleTask");
        eopsTask.execute();*/

        try {
            HttpServer httpServer = HttpServer.create(new InetSocketAddress(Integer.parseInt(autoCheckPort)), 0);
            httpServer.createContext("/syncAutoCheck", new AutoCheckHandler());
            httpServer.start();
            System.out.println(autoCheckPort +  "端口已成功启动");
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<Dict> intervals = getIntervals();

        for(Dict interval : intervals){
            //每个周期开启一个周期线程用于调度周期任务
            EopsTemplateScheduleTask eopsTask = SpringUtils.getBean("eopsTemplateScheduleTask");
            eopsTask.setInterval(interval.getValue());
            eopsTask.setTemplateTypes("3,6,5");

            quartzTableManager.addManualTaskJob("EopsTemplateSchedule" + interval.getValue(),
                    "EopsTemplateSchedule", "EopsTemplateSchedule" + interval.getValue(),
                    "EopsTemplateSchedule", ManualQuartzJob.class, getCronByInterval(interval.getValue()),eopsTask);
        }
    }
    class AutoCheckHandler implements HttpHandler {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            //返回
            String rtn="success";
            httpExchange.sendResponseHeaders(200, 0);
            //获取请求路径
            URI requestURI = httpExchange.getRequestURI();
            String param = requestURI.getQuery();
            execSync(param);
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(rtn.getBytes());
            responseBody.close();
        }

    }

    public void execSync(String type){
        type = type.split("=")[1];
        System.out.println(type);
        System.out.println(dictService);
        List<Dict> intervals = getIntervals();
        System.out.println(intervals.size());

        for(Dict interval : intervals){
            EopsTemplateScheduleTask eopsTask = SpringUtils.getBean("eopsTemplateScheduleTask");
            //每个周期创建一个对象
            System.out.println("EopsTemplateScheduleTask : " + eopsTask);
            eopsTask.setInterval(interval.getValue());
            eopsTask.setTemplateTypes("'" + type + "'");
            eopsTask.execute();
        }
    }

    private static  List<Dict> getIntervals() {
        List<Dict> intervals = new ArrayList<>();
        Dict d1 = new Dict(); d1.setValue("5");  //5分
        Dict d2 = new Dict(); d2.setValue("15");  //15分
        Dict d3 = new Dict(); d3.setValue("30");  //30分
        Dict d4 = new Dict(); d4.setValue("60");  //1小时
        Dict d5 = new Dict(); d5.setValue("1440"); //1天
        Dict d6 = new Dict(); d6.setValue("10080"); //1周
        intervals.add(d1);
        intervals.add(d2);
        intervals.add(d3);
        intervals.add(d4);
        intervals.add(d5);
        intervals.add(d6);
        return intervals;
    }

    private static String getCronByInterval(String interval){
        switch (interval){
            case "5":return "0 0/5 * * * ?";
            case "30":return "0 0/30 * * * ?";
            case "60":return "0 0 0/1 * * ?";
            case "1440":return "0 0 0 * * ?";
            case "10080":return "0 0 0 ? * 1";
            default:return "0 0/15 * * * ?";
			/*case "5":return "0/5 * * * * ?";
			case "30":return "0/30 * * * * ?";
			case "60":return "0 0 0/1 * * ?";
			case "1440":return "0 0 0 * * ?";
			case "10080":return "0 0 0 ? * 1";
			default:return "0/15 * * * * ?";*/
        }

    }
}
